package com.atools;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class AndroidCalendar {

	public class CalendarInfo {
		public String id;
		public String name;
	};

	private List<CalendarInfo> mCalendars = new ArrayList<CalendarInfo>();
	private Activity mActivity;
	private String mCalendarUri = "content://com.android.calendar";
	private String mRemindersUri = "content://com.android.calendar/reminders";


	public AndroidCalendar(Activity activity) {
		mActivity = activity;

		String[] projection = new String[] { "_id", "name" };
		Uri calendarsUri = Uri.parse(mCalendarUri + "/calendars");
		Cursor managedCursor = activity.getContentResolver().query(
				calendarsUri, projection, null, null, null);
		if (managedCursor != null && managedCursor.moveToFirst()) {
			String calName;
			CalendarInfo calendarInfo;
			int nameColumn = managedCursor.getColumnIndex("name");
			int idColumn = managedCursor.getColumnIndex("_id");
			do {
				calName = managedCursor.getString(nameColumn);
				if (calName != null) {
					calendarInfo = new CalendarInfo();
					calendarInfo.id = managedCursor.getString(idColumn);
					calendarInfo.name = calName;
					mCalendars.add(calendarInfo);

					Log.d("cal", "name: " + calendarInfo.name + " id: "
							+ calendarInfo.id);
				}
			} while (managedCursor.moveToNext());
			managedCursor.close();
		}


		//		Uri eventsURI = Uri.parse(mCalendarUri + "/events");
		//
		//		managedCursor = activity.managedQuery(
		//				eventsURI, null, null, null, null);
		//		if (managedCursor != null && managedCursor.moveToFirst()) {
		//			String cols = "";
		//			do {
		//				for (String col : managedCursor.getColumnNames()) {
		//					int columnId = managedCursor.getColumnIndex(col);
		//
		//					cols += col + ": " + managedCursor.getString(columnId) + ",";
		//				}
		//				cols += "\n";
		//			} while (managedCursor.moveToNext());
		//			Log.d("columns: ", cols);
		//		}

	}

	public List<CalendarInfo> getCalendars() {
		return mCalendars;
	}

	public Uri addEvent(String calendarId, String title, String description,
			String location, Date start, Date end) {

		ContentValues event = new ContentValues();
		event.put("eventTimezone",
				Calendar.getInstance().get(Calendar.ZONE_OFFSET));
		event.put("calendar_id", calendarId);
		event.put("title", title);
		if (description != null) {
			event.put("description", description);
		}
		if (location != null) {
			event.put("eventLocation", location);
		}
		if (start != null) {
			event.put("dtstart", start.getTime());
		}
		if (end != null) {
			event.put("dtend", start.getTime());
		}

		Uri result = null;
		Uri eventsURI = Uri.parse(mCalendarUri + "/events");
		try {
			result = mActivity.getContentResolver().insert(eventsURI, event);
			Log.d("CALTEST", result.toString());
		} catch (Exception e) {
			Log.e("CALTEST", e.toString());
		}

		return result;
	}

	public Uri addReminder(Uri eventUri, int reminderMinutes) {
		// add reminder
		String eventId = eventUri.getLastPathSegment();
		ContentValues reminder = new ContentValues();
		reminder.put("minutes", reminderMinutes);
		reminder.put("event_id", eventId);
		reminder.put("method", 1); // alert

		Uri reminderUri = mActivity.getContentResolver().insert(
				Uri.parse(mRemindersUri), reminder);

		Log.d("reminderUri", reminderUri.toString());
		return reminderUri;
	}

	public void removeEvent(String calendarId, String title, String description,
			String location, Date start, Date end) {
		Uri eventsURI = Uri.parse(mCalendarUri + "/events");

		List<Long> ids = new ArrayList<Long>();
		String[] projection = new String[] { "_id" };

		String selection =
				"calendar_id = " + calendarId +
				" AND title = '" +  title + "'";
		if (description != null) {
			selection += " AND description = '" + description + "'";
		}
		if (location != null) {
			selection += " AND eventLocation = '" + location + "'";
		}
		if (start != null) {
			selection += " AND dtstart = " + start.getTime();
		}
		if (end != null) {
			selection += " AND dtend = " + start.getTime();
		}

		Cursor cursor = mActivity.getContentResolver().query(
				eventsURI, projection, selection, null,
				null);
		if (cursor != null && cursor.moveToFirst()) {
			int idColumn = cursor.getColumnIndex("_id");

			do {
				ids.add(cursor.getLong(idColumn));
			} while (cursor.moveToNext());
			cursor.close();
		}

		for (Long id : ids) {
			Log.d("cal", "deleting event with id: " + id);
			Uri uri = ContentUris.withAppendedId(eventsURI, id);
			mActivity.getContentResolver().delete(uri, null, null);
		}
	}
}
